﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.fminutes.vinciNet {
    import flash.events.*;
    import flash.utils.*;
    import flash.net.*;
    import com.adobe.serialization.json.*;

    public class Statistics extends EventDispatcher {

        private var uid:int;
        private var sampling:Boolean = false;
        private var samplingProportion:Number = 1;
        private var clockInterval:uint = 0;
        private var timer:Timer;
        protected var clocking:Boolean = false;
        private var _httpUrl:String = "";
        private var urlRequest:URLRequest;
        private var urlLoader:URLLoader;
        private var urlRequestList:Array;
        private var lastRequestCompleted:Boolean = true;

        public function Statistics(){
            this.urlRequest = new URLRequest();
            this.urlLoader = new URLLoader();
            this.urlRequestList = [];
        }
        public function setProperty(_arg1:String, _arg2:Boolean=false, _arg3:Boolean=false, _arg4:Number=1, _arg5:uint=0):void{
            this.uid = this.cal(_arg1);
            this.sampling = _arg2;
            this.samplingProportion = _arg4;
            this.clocking = _arg3;
            this.clockInterval = _arg5;
            if (_arg3){
                this.clockStart();
                this.timer = new Timer(_arg5);
                this.timer.addEventListener(TimerEvent.TIMER, this.clockIntervalHandler);
                this.timer.start();
            };
        }
        private function cal(_arg1:String):uint{
            var _local2:int = int(_arg1.substr((_arg1.length - 5), 5));
            return (_local2);
        }
        public function get httpUrl():String{
            return (this._httpUrl);
        }
        public function set httpUrl(_arg1:String):void{
            this._httpUrl = _arg1;
        }
        protected function clockStart():void{
        }
        protected function clockIntervalHandler(_arg1:TimerEvent):void{
        }
        protected function sendStat(_arg1:String, _arg2:Object=null):void{
            if (((this.sampling) && ((this.samplingProportion < 1)))){
                if ((this.uid % (1 / this.samplingProportion)) != 0){
                    return;
                };
            } else {
                this.sendStatHandler(_arg1, _arg2);
            };
        }
        private function sendStatHandler(_arg1:String, _arg2:Object=null):void{
            var _postData:* = null;
            var isHandler:* = false;
            var ioerrorHandler:* = null;
            var httpStatusHandler:* = null;
            var loadCompHandler:* = null;
            var _uv:* = null;
            var _name:* = null;
            var StatType:* = _arg1;
            var data = _arg2;
            if (!this.lastRequestCompleted){
                this.urlRequestList.push({
                    StatType:StatType,
                    data:data
                });
                return;
            };
            var dataJson:* = JSON.encode(data);
            var dataObj:* = {data:dataJson};
            if ((dataObj is String)){
                _postData = dataObj;
            } else {
                if ((dataObj is URLVariables)){
                    _postData = dataObj;
                } else {
                    _uv = new URLVariables();
                    for (_name in dataObj) {
                        _uv[_name] = dataObj[_name];
                    };
                    _postData = _uv;
                };
            };
            this.urlRequest.url = (this._httpUrl + StatType);
            this.urlRequest.method = URLRequestMethod.POST;
            this.urlRequest.data = _postData;
            isHandler = false;
            ioerrorHandler = function (_arg1:IOErrorEvent):void{
                if (!isHandler){
                    isHandler = true;
                };
            };
            httpStatusHandler = function (_arg1:HTTPStatusEvent):void{
                if (!isHandler){
                    isHandler = true;
                };
            };
            loadCompHandler = function (_arg1:Event):void{
                if (!isHandler){
                    isHandler = true;
                };
                lastRequestCompleted = true;
                clearAll();
                if (((!((urlRequestList == null))) && ((urlRequestList.length > 0)))){
                    sendStatHandler(urlRequestList[0]["StatType"], urlRequestList[0]["data"]);
                    urlRequestList.shift();
                };
            };
            var clearAll:* = function ():void{
                urlLoader.close();
                urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioerrorHandler);
                urlLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                urlLoader.removeEventListener(Event.COMPLETE, loadCompHandler);
            };
            this.urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioerrorHandler);
            this.urlLoader.addEventListener(Event.COMPLETE, loadCompHandler);
            this.urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            this.urlLoader.dataFormat = "text";
            this.lastRequestCompleted = false;
            this.urlLoader.load(this.urlRequest);
        }

    }
}//package com.fminutes.vinciNet 
